
package com.wobaby.util.config;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.wobaby.util.RenditionUncheckedException;

// Referenced classes of package com.wobaby.util.config:
//            OptionHistory, Config, ConfigRetriever, FileConfig

public class ConfigOptionsDiff
    implements Serializable
{

    public ConfigOptionsDiff()
    {
        _changedOptions = new HashMap();
    }

    public void putChange(Object key, OptionHistory value)
    {
        _changedOptions.put(key, value);
    }

    public String getAccountingString()
    {
        StringBuffer buf = new StringBuffer(2048);
        for(Iterator it = _changedOptions.keySet().iterator(); it.hasNext();)
        {
            Object key = it.next();
            OptionHistory optionHistory = (OptionHistory)_changedOptions.get(key);
            boolean inOld = optionHistory.oldValue != null;
            boolean inNew = optionHistory.currentValue != null;
            if(!inOld && inNew)
            {
                if(buf.length() > 0)
                    buf.append("\n");
                buf.append("Added: ").append(key);
                buf.append("=").append(optionHistory.currentValue);
            } else
            if(inOld && !inNew)
            {
                if(buf.length() > 0)
                    buf.append("\n");
                buf.append("Removed: ").append(key);
                buf.append("=").append(optionHistory.oldValue);
            } else
            if(inOld && inNew)
            {
                if(buf.length() > 0)
                    buf.append("\n");
                buf.append("Changed: ").append(key).append(" ");
                buf.append(optionHistory.oldValue).append("=>");
                buf.append(optionHistory.currentValue);
            } else
            {
                throw new RenditionUncheckedException("Somehow wound up with an OptionHistory instance that contains neither an old value, nor a new value. " );
            }
        }

        return buf.toString();
    }

    public Iterator getTransferableChanges()
    {
        List coreSpecificOptions = Config.getConfig().getList("distributed/core-specific-options");
        FileConfig.debug("[ConfigOptionsDiff.getTransferableChanges()] coreSpecificOptions: [" + coreSpecificOptions + "]");
        HashMap transferableChanges = new HashMap(_changedOptions.size());
        Iterator it = _changedOptions.keySet().iterator();
        do
        {
            if(!it.hasNext())
                break;
            String key = (String)it.next();
            FileConfig.debug("[ConfigOptionsDiff.getTransferableChanges()] key: [" + key + "]");
            if(!coreSpecificOptions.contains(key))
                transferableChanges.put(key, _changedOptions.get(key));
        } while(true);
        return transferableChanges.keySet().iterator();
    }

    public Set getChangedOptions()
    {
        return _changedOptions.keySet();
    }

    public Object getOptionValue(String optionName)
    {
        Object optionValue = null;
        OptionHistory history = (OptionHistory)_changedOptions.get(optionName);
        if(history != null)
            optionValue = history.currentValue;
        return optionValue;
    }

    public Date getOptionChangedDate(String optionName)
    {
        Date lastModifiedDate = null;
        OptionHistory history = (OptionHistory)_changedOptions.get(optionName);
        if(history != null)
            lastModifiedDate = history.lastModifiedDate;
        return lastModifiedDate;
    }

    public OptionHistory getOptionHistory(String optionName)
    {
        return (OptionHistory)_changedOptions.get(optionName);
    }

    private static final long serialVersionUID = 0x1dc30ed4af08fa24L;
    HashMap _changedOptions;
}